package com.ulook.lookplanet.ui.notifications.UserDetail;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.ulook.lookplanet.R;
import com.ulook.lookplanet.base.BaseViewModel2;
import com.ulook.lookplanet.common.AppServer;
import com.ulook.lookplanet.data.model.BaseUser;
import com.ulook.lookplanet.data.model.TCornUser;
import com.ulook.lookplanet.data.model.TUser;
import com.ulook.lookplanet.ui.home.VUserInfo;
import com.ulook.lookplanet.util.ThrowException;
import com.ulook.lookplanet.util.net.AsynRequestUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.RequestParams;
import com.ulook.lookplanet.util.net.ResponseCallback;

/**
 * 描述：
 *
 * @author L1998
 * @data 2021/5/1
 */
public class UserDetailViewModel extends BaseViewModel2 {

    private int userId;
    private int currUser;
    private MutableLiveData<VUserInfo> userInfo = new MutableLiveData<>();
    private MutableLiveData<TCornUser> cornUser = new MutableLiveData<>();

    public LiveData<VUserInfo> getUserInfo() {
        return userInfo;
    }
    public LiveData<TCornUser> getCornUser() {
        return cornUser;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public int getCurrUser() {
        return currUser;
    }

    public void setCurrUser(int currUser) {
        this.currUser = currUser;
    }

    /**
     * 获取用户的信息
     */
    public boolean initUserInfo(){
        // 游客
        if(userId == 0){
            setToastText("用户不存在");
            return false;
        // 用户
        }else {
            ResponseCallback callback = new ResponseCallback() {
                @Override
                public void onSuccess(Object res) {
                    userInfo.setValue((VUserInfo) res);
                }
                @Override
                public void onFailure(OkHttpException failuer) {
                    setToastText("获取用户信息失败");
                    ThrowException.print(failuer, "获取用户信息失败");
                }
            };
            RequestParams params = new RequestParams();
            params.put("id", userId);
            AsynRequestUtil.post(AppServer.ROOT_PATH + "/home/getUserInfo", params, VUserInfo.class, callback);
        }
        return true;
    }

    /**
     * 是否关注
     */
    public void isCornUser(){
        if(userId != 0 && currUser != 0){
            ResponseCallback callback = new ResponseCallback() {
                @Override
                public void onSuccess(Object res) {
                    TCornUser cornUserTem = (TCornUser) res;
                    if(cornUserTem != null){
                        cornUser.setValue(cornUserTem);
                    }else {
                        cornUser.setValue(null);
                    }
                }
                @Override
                public void onFailure(OkHttpException failuer) {
                    cornUser.setValue(null);
                }
            };
            RequestParams params = new RequestParams();
            params.put("userId", currUser);
            params.put("cornUserId", userId);
            AsynRequestUtil.post(AppServer.ROOT_PATH + "/home/isCornUser", params, TCornUser.class, callback);
        }
    }

    /**
     * 点击关注
     */
    public void beCornUser(){
        if(userId != 0 && currUser != 0){
            RequestParams params = new RequestParams();
            params.put("userId", currUser);
            params.put("cornUserId", userId);
            AsynRequestUtil.post(AppServer.ROOT_PATH + "/home/beCornUser", params, null, null);
            if(cornUser.getValue() == null){
                cornUser.setValue(new TCornUser());
            }else {
                cornUser.setValue(null);
            }
        }else {
            setToastText("未登录");
        }
    }

}
